#ifndef TREE_ROOT_H
#define TREE_ROOT_H

#include "TreeNode.h"
#include "Color.h"
#include <list>

namespace ColorTree
{
    class TreeRoot
    {
    private:
        TreeNode* root = nullptr;
    public:
        inline TreeNode& GetRoot()
        {
            if (root == nullptr)
                root = new TreeNode();
            return *root;
        }
        inline void AddColor(Color c)
        {
            GetRoot()
                    .GetOrCreatSubNode(c.GetB0())
                    .GetOrCreatSubNode(c.GetB1())
                    .GetOrCreatSubNode(c.GetB2())
                    .GetOrCreatSubNode(c.GetB3())
                    .GetOrCreatSubNode(c.GetB4())
                    .GetOrCreatSubNode(c.GetB5())
                    .GetOrCreatSubNode(c.GetB6())
                    .GetOrCreatSubNode(c.GetB7())
                    .AddCounter();
        }
        inline void ReduceColor()
        {

        }
        inline std::list<Color> ToList()
        {
            std::list<Color> cl;
            for (int i0 = 0; i0 < 8; i0++)
            {
                auto node0 = GetRoot().GetSubNode(i0);
                if (node0 == nullptr)
                    continue;
                for (int i1 = 0; i1 < 8; i1++)
                {
                    auto node1 = node0->GetSubNode(i1);
                    if (node1 == nullptr)
                        continue;
                    for (int i2 = 0; i2 < 8; i2++)
                    {
                        auto node2 = node1->GetSubNode(i2);
                        if (node2 == nullptr)
                            continue;
                        for (int i3 = 0; i3 < 8; i3++)
                        {
                            auto node3 = node2->GetSubNode(i3);
                            if (node3 == nullptr)
                                continue;
                            for (int i4 = 0; i4 < 8; i4++)
                            {
                                auto node4 = node3->GetSubNode(i4);
                                if (node4 == nullptr)
                                    continue;
                                for (int i5 = 0; i5 < 8; i5++)
                                {
                                    auto node5 = node4->GetSubNode(i5);
                                    if (node5 == nullptr)
                                        continue;
                                    for (int i6 = 0; i6 < 8; i6++)
                                    {
                                        auto node6 = node5->GetSubNode(i6);
                                        if (node6 == nullptr)
                                            continue;
                                        for (int i7 = 0; i7 < 8; i7++)
                                        {
                                            auto node7 = node6->GetSubNode(i7);
                                            if (node7 == nullptr)
                                                continue;
                                            cl.emplace_back(Color(i0, i1, i2, i3, i4, i5, i6, i7));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return cl;
        }
    };
}

#endif //TREE_ROOT_H
